package io.sage.modules.basics.controller;

import java.util.*;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.sage.common.utils.R;
import io.sage.modules.basics.entity.BGoodsUnit;
import io.sage.modules.basics.service.BGoodsUnitService;
import io.sage.modules.sys.entity.SysUserEntity;
import io.sage.modules.sys.shiro.ShiroUtils;



/**
 * 物品单位
 *
 * @author xujh
 * @email jhang_mail@163.com
 * @date 2019-07-08 22:38:05
 */
@RestController
@RequestMapping("basics/bgoodsunit")
public class BGoodsUnitController {
    @Autowired
    private BGoodsUnitService bGoodsUnitService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("basics:bgoodsunit:list")
    public R list(@RequestParam Map<String, Object> params){
    	Integer pageSize = 1;
		if (params != null && params.get("limit") != null) {
			pageSize = java.lang.Integer.parseInt(params.get("limit").toString());
		}
		Integer pageNum = 10;
		if (params != null && params.get("page") != null) {
			pageNum = java.lang.Integer.parseInt(params.get("page").toString());
		}

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		if(params.get("antennaName")!=null){

			if(!"".equals(params.get("antennaName").toString())){
				paramMap.put("antennaName", params.get("antennaName").toString());
			}
		}
		paramMap.put("orderby", "gu.update_time desc,gu.create_time desc");
		PageHelper.startPage(pageNum, pageSize);// 设置分页的起始码以及页面大小
		List<BGoodsUnit> selectGoodsUnitList = bGoodsUnitService.selectGoodsUnitList(paramMap);

		PageInfo<BGoodsUnit> pageInfo = new PageInfo<BGoodsUnit>(selectGoodsUnitList);// 传入list就可以了

		return R.ok().put("goodsUnitList", pageInfo);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("basics:bgoodsunit:info")
    public R info(@PathVariable("id") Integer id){
    	BGoodsUnit goodsUnit = bGoodsUnitService.selectByPrimaryKey(id);
		Map<String, Object> paramMap = new HashMap<String, Object>();

		paramMap.put("goodsUnit", goodsUnit);

		return R.ok().put("map", paramMap);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("basics:bgoodsunit:save")
    public R save(@RequestBody JSONObject jsonObject){
    	BGoodsUnit goodsUnit = jsonObject.getObject("goodsUnit", BGoodsUnit.class);

		SysUserEntity user = ShiroUtils.getUserEntity();
		goodsUnit.setCreateUserId(user.getUserId().intValue());
		int insert = bGoodsUnitService.insert(goodsUnit);

		if(insert>0){
			return R.ok();
		}

		return R.error();
	}

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("basics:bgoodsunit:update")
    public R update(@RequestBody JSONObject jsonObject){
    	BGoodsUnit goodsUnit = jsonObject.getObject("goodsUnit", BGoodsUnit.class);
		
		if(goodsUnit.getId()==null){
			return R.error("需要修改的id，不能为空");
		}
		int update = bGoodsUnitService.updateByPrimaryKeySelective(goodsUnit);//全部更新

		if(update>0){
			return R.ok();
		}
		return R.error();
	}

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("basics:bgoodsunit:delete")
    public R delete(@RequestBody Integer[] ids){


		List<Integer> idslist=new ArrayList<Integer>(Arrays.asList(ids));
		Map<String,Object> map=new HashMap<>();
		List<BGoodsUnit>list=bGoodsUnitService.selectIsUse(ids);
		StringBuffer stringBuffer=new StringBuffer();
		for (BGoodsUnit bGoodsUnit:list) {
			stringBuffer.append(bGoodsUnit.getAntennaName());
			idslist.remove(bGoodsUnit.getId());
			stringBuffer.append(",");
		}

		ids=idslist.toArray(new Integer[idslist.size()]);
		int delete=0;
		if(ids.length>0){
			delete= bGoodsUnitService.deleteBatchIds(ids);
		}
		if(list.size()>0){
			String msg=stringBuffer.toString().substring(0,stringBuffer.toString().length() - 1);
			map.put("msg",msg+"存在使用，无法删除");
		}else{
			map.put("msg","删除成功");
		}
		if(delete==ids.length){
			return R.ok().put("data",map);
		}
    	/*int delete = bGoodsUnitService.deleteBatchIds(ids);

		if(delete>0){
			return R.ok();
		}
*/
		return R.error();
    }

}
